<!DOCTYPE html>
<html>
<!--
  Copyright 2010-2015 Brian McKelvey.
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<head>
  <meta charset="utf-8">
  <title>Firefox Bug</title>
  <script>
  
  var socket;
  
  var requestedLength;
  
  function connect() {
    if (socket && socket.readyState === WebSocket.OPEN) {
      socket.close();
    }
    socket = new WebSocket(get_appropriate_ws_url(), 'fragmentation-test');
    socket.onopen = handleSocketOpen;
    socket.onmessage = handleSocketMessage;
    socket.onclose = handleSocketClose;
    socket.onerror = handleSocketError;
  }
  
  function disconnect() {
    socket.close();
  }
  
  function clearLog() {
    
  }
  
  function clearLog() {
    var el = document.getElementById('output');
    el.innerHTML = '';
  }
  
  function log(data) {
    // if (window['console'] && window['console']['log'])
    //   console.log(data)

    var el = document.getElementById('output');
    el.innerHTML += ('<br>\n' + data);
  }
  
  function requestData() {
    socket.send('sendMessage|' + requestedLength);
    requestedLength += Math.ceil(Math.random() * 1024);
  }
  
  function handleSocketOpen() {
    log("Socket opened.");
    requestedLength = 100;
    requestData();
  }

  function handleSocketMessage(event) {
    log("Received " + event.data.length + " character utf-8 response from server.");
    requestData();
  }
  
  function handleSocketClose() {
    log("Socket closed.");
  }
  
  function handleSocketError() {
    log("Socket error.")
  }

  function get_appropriate_ws_url()
  {
  	var pcol;
  	var u = document.URL;

  	/*
  	 * We open the websocket encrypted if this page came on an
  	 * https:// url itself, otherwise unencrypted
  	 */

  	if (u.substring(0, 5) == "https") {
  		pcol = "wss://";
  		u = u.substr(8);
  	} else {
  		pcol = "ws://";
  		if (u.substring(0, 4) == "http")
  			u = u.substr(7);
  	}

  	u = u.split('/');

  	return pcol + u[0];
  }
  
  </script>
</head>
<body>
  <div>
    <h1>Controls</h1>
    <button onclick="clearLog()">Clear Log</button>
    <button onclick="connect()">Connect</button>
    <button onclick="disconnect()">Disconnect</button><br><br>
  </div>
  <div>
    <h1>Output</h1>
    <div id="output">Ready.</div>
  </div>
</body>
</html>
